﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace sx_windows.utils
{
    public static class Tool
    {
        public static long GetTimestampMills()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalMilliseconds);
        }
        public static long GetTimestamp()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalMilliseconds) / 1000;
        }
        public static string Bytes2string(byte[] b)
        {
            return System.Text.Encoding.Default.GetString(b);
        }
        public static byte[] String2bytes(string str)
        {
            return System.Text.Encoding.Default.GetBytes(str);
        }
        public static class MD5
        {
            public static byte[] Encode(byte[] str)
            {
                System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
                byte[] data = md5.ComputeHash(str);
                string am = "";
                for (int h = 0; h < data.Length; h++)
                {
                    am = am + data[h].ToString("x2");
                }
                byte[] data32 = new byte[32];
                data32 = String2bytes(am);
                md5.Clear();
                return data32;
            }
            public static string Encode2string(byte[] str)
            {
                return Bytes2string(Encode(str));
            }
            public static byte[] Encode(string str)
            {
                return Encode(String2bytes(str));
            }
            public static string Encode2string(string str)
            {
                return Bytes2string(Encode(str));
            }
        }

    }
}
